package com.nbgs.demo.service.impl;


import com.nbgs.demo.dao.ShowUserMapper;
import com.nbgs.demo.info.AllInfo;
import com.nbgs.demo.info.LastConsumptionInfo;
import com.nbgs.demo.info.ShowUserEnteringInfo;
import com.nbgs.demo.info.UserInfo;
import com.nbgs.demo.service.ShowUserServiceInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ShowUserServiceInterfaceImpl implements ShowUserServiceInterface {

    @Autowired
    private ShowUserMapper showUserMapper;

    @Override
    public AllInfo showUser(String workerNum,int meterId,String type,int currentPage) {
        AllInfo allInfo = new AllInfo();
        allInfo.setSize(2);
        int size  = allInfo.getSize();

        String areaNum = showUserMapper.getAreaNumByWorkerNum(workerNum);

        log.info("当前showUserService层，参数为："+areaNum+","+meterId+","+type);

        List<Integer> userId = showUserMapper.getUserIdByUserInfo(areaNum,meterId,type,size*(currentPage-1));

        log.info("当前showUserService层，userID为："+userId.toString());

        List<ShowUserEnteringInfo> userEnteringInfos = new ArrayList<>(10);

        ShowUserEnteringInfo userEnteringInfo = null;

        for (int id: userId) {
            userEnteringInfo = new ShowUserEnteringInfo();

            log.info("用户id："+id);

            userEnteringInfo.setUserId(id);

            UserInfo userInfo = showUserMapper.getUserInfo(id);

            log.info("用户信息："+userInfo);

            BeanUtils.copyProperties(userInfo,userEnteringInfo);

            LastConsumptionInfo lastConsumptionInfo = showUserMapper.selectLastConsumptionInfo(id);

            log.info("上次的用水信息状态："+lastConsumptionInfo.isConsumptionState());

            BeanUtils.copyProperties(lastConsumptionInfo,userEnteringInfo);

            boolean b = lastConsumptionInfo.isConsumptionState();

            log.info("当前信息录入状态："+b);

            if(!b){
                userEnteringInfo.setConsumptionState(false);
                userEnteringInfo.setBillState(false);
            }else{
                String billState = showUserMapper.getBillState(id);
                //log.info("已录入信息后的账单状态："+billState);
                if(null==billState){
                    userEnteringInfo.setConsumptionState(true);
                    userEnteringInfo.setBillState(false);
                }else if(billState.equals("已缴")){
                    userEnteringInfo.setConsumptionState(true);
                    userEnteringInfo.setBillState(true);
                }
            }

            userEnteringInfos.add(userEnteringInfo);
        }



        allInfo.setList(userEnteringInfos);
        int total = showUserMapper.getUserIdCount(areaNum,meterId,type);
        allInfo.setTotal(total);
        int totalPage = total%size==0?total/size:total/size+1;
        allInfo.setTotalPage(totalPage);

        log.info("当前showUserService层，参数为："+userEnteringInfos);

        return allInfo;
    }

}
